home *** CD-ROM | disk | FTP | other *** search
- /* Functions for getting Finder information about files and directories.
-
- 93/10/13 AIH
- - added FileIsFolder
- - removed unnecessary functions
- - added functions to get a file's creator and type
-
- 91/05/27 AIH
- - added a few comments
-
- 91/05/06 AIH
- - Wrote functions for getting and setting all of a file's information
- - Fixed bug when setting the extra Finder information
-
- 91/04/21 AIH
- - Function for testing if a file exists returns an error code if the
- file doesn't exist, instead of setting a flag. */
-
- #include <limits.h>
- #include <string.h>
- #include "pstr.h"
- #include "MacLib.h"
- #include "MemoryLib.h"
- #include "StringLib.h"
- #include "FileLib.h"
-
- /*----------------------------------------------------------------------------*/
- /* getting catalog information about files and directories */
- /*----------------------------------------------------------------------------*/
-
- /* get information for file */
- void FileInfoGet(FileType *fp, FInfo *info)
- {
- require(FileValid(fp));
- FailOSErr(HGetFInfo(fp->vol, fp->dir, fp->pnm, info));
- }
-
- /* get information for file */
- void FileInfoSet(FileType *fp, FInfo *info)
- {
- require(FileValid(fp));
- FailOSErr(HSetFInfo(fp->vol, fp->dir, fp->pnm, info));
- }
-
- /* return the file's creator */
- OSType FileCreator(FileType *fp)
- {
- FInfo info;
- FileInfoGet(fp, &info);
- return(info.fdCreator);
- }
-
- /* return the file's type */
- OSType FileTypeGet(FileType *fp)
- {
- FInfo info;
- FileInfoGet(fp, &info);
- return(info.fdType);
- }
-
- /* get the file's catalog */
- void FileCatalog(FileType *fp, CInfoPBRec *pb)
- {
- require(FileValid(fp));
- memclr(pb, sizeof(CInfoPBRec));
- if (*fp->pnm)
- pb->hFileInfo.ioNamePtr = fp->pnm;
- pb->hFileInfo.ioVRefNum = fp->vol;
- pb->hFileInfo.ioDirID = fp->dir;
- FailOSErr(PBGetCatInfo(pb, false));
- }
-
- /* set the file's catalog */
- void FileCatalogSet(FileType *fp, CInfoPBRec *pb)
- {
- require(FileValid(fp));
- pb->hFileInfo.ioNamePtr = fp->pnm;
- pb->hFileInfo.ioVRefNum = fp->vol;
- pb->hFileInfo.ioDirID = fp->dir;
- FailOSErr(PBSetCatInfo(pb, false));
- }
-
- /*----------------------------------------------------------------------------*/
- /* getting other information about files and directories */
- /*----------------------------------------------------------------------------*/
-
- /* Given a file structure specifying a directory, sets the file's
- directory ID to the ID of the named directory and clears
- the file's name:
- Struct Input Output
- fp->vol Volume reference number --> Volume reference number
- fp->dir Parent directory ID --> Directory ID
- fp->cnm Name of directory --> (null)
- fp->pnm Name of directory --> (null)
- This function is most useful for moving into the offspring of a
- directory without using partial pathnames: simply follow the call
- to this function with a call to FileNameSet and then to FileResolve. */
- void FileDirID(FileType *fp)
- {
- CInfoPBRec pb;
-
- require(FileValid(fp));
- FileCatalog(fp, &pb);
- if ((pb.dirInfo.ioFlAttrib & (1 << kFolderBit)) == 0)
- FailOSErr(fnfErr); /* not a directory */
- fp->dir = pb.dirInfo.ioDrDirID;
- *fp->cnm = *fp->pnm = 0;
- }
-
- /* Return true a is the same as b. Notice that two files are only
- considered identical if they are on the same volume or in the same
- working directory, in addition to having identical names and
- directory IDs. */
- Boolean FilesAreSame(FileType *a, FileType *b)
- {
- require(FileValid(a));
- require(FileValid(b));
- return(a->vol == b->vol && a->dir == b->dir &&
- EqualString(a->pnm, b->pnm, false, true));
- }
-
- /* true if the file exists */
- Boolean FileExists(FileType *fp)
- {
- CInfoPBRec pb;
- volatile Boolean exists = true;
-
- TRY {
- if (exists) {
- FileCatalog(fp, &pb);
- exists = ((pb.hFileInfo.ioFlAttrib & (1 << kFolderBit)) == 0);
- }
- } CATCH {
- if (FailReason() == fnfErr) {
- exists = false;
- RETRY;
- }
- } ENDTRY;
- return(exists);
- }
-